WCF service is independent of the hosting
environment, allowing the most appropriate hosting model to be selected
without the need to change anything in the service.
There are several options when it comes to hosting WCF services:
Internet Information Services (IIS)
Windows Activation Service (WAS) installed with IIS 7.0
any managed application process, including console, Windows Forms, Windows Presentation Foundation (WPF)
any managed Windows service applications
The ServiceHost class can be used to host a WCF service when IIS or WAS is not used. The ServiceHost
instance must be initialized with the base address and one or more
endpoints that include binding and contract parameters, as shown in the
following example:
Example 1.
ServiceHost host = new ServiceHost(typeof(IService)); host.AddEndpoint(typeof(IService),Binding, Uri); host.Open();
|
The ServiceHost
constructor is provided with the service type and an array of base
addresses. Each base address must have a different transport specified
in its URI. The actual address of each endpoint is derived relative to
the base address. If there is more than one base address associated
with the ServiceHost instance, the
appropriate one is used based on the transport protocol. The base
addresses are typically stored and retrieved from the application’s
configuration file.
An application domain is a
construct of a CLR that is the unit of isolation of an application. The
main purpose of the hosting environment is to provide a worker process
in an application domain for the ServiceHost to run.
The hosting environment is a
process and runs as an identity. Therefore, it also provides a default
security context to the service being hosted. Some hosting environments
are more sophisticated than others and can provide features such as
process recycling or application restart on failure.
Design considerations to be mindful of when choosing a hosting environment include:
availability
reliability
manageability
deployment
All
hosting environments in the following list support the application of
service-orientation, so choosing the correct environment comes down to
addressing the type of transport and security parameters used to expose
a service, scalability requirements, and SLA requirements.
Note that any service must be hosted within a runtime environment that creates and controls its context and lifetime.